package com.suitsoft.ut.web.middle;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.springside.modules.web.struts2.annotations.Token;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.commons.Weeklydiary;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.commons.WeeklydiaryManager;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 *  写周记
 *@author Suineg
 */
@Results({@Result(name=CrudActionSupport.RELOAD,location="weeklydiary-write",type="redirect")})
public class WeeklydiaryWriteAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -498686159802673643L;
	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	//Action's Result
	/** 周记 */
	private final String DIARY = "diary";
	
	
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** 流程管理  服务层对象 */
	private ThesisFileManager fileManager;
	/** 周记管理 服务层对象 */
	private WeeklydiaryManager weeklydiaryManager;
	/** 学生管理 服务层对象 */
	private StudentManager studentManager;
	
	/** 题的分页 */
	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 状态 */
	private Long stauts;
	/** 标识  1 - 评阅,2 - 团队,3 - 专家，4 - 基层 (专家没有评语。所以，专家审核时不会用到这个.)*/
	private Long  flag;
	/** 题目 */
	private String titile;
	/** 教师名 */
	private String teacherName;
	/** 当前登陆用户 */
	private LoginUser loginUser;
	/** 周记ID */
	private Long wid;
	/** 周记实体 */
	private Weeklydiary weeklydiary;
	/** 周记类型  即 第几周的周记  1 3 5 7 9 类推  20为总结*/
	private Long diaryType;
	/** diaryType的有效值集合 */
	private Long[] effievtiveValue = new Long[]{1L,3L,5L,7L,9L,11L,13L,15L,20L}; 
	/***/
	private List<Thesis> listAll;
	
	@Override
	public String execute() throws Exception {
		loginUser = UserHolder.getLoginUser();
		Student student = studentManager.findByUser(UserHolder.getLoginUser().getId());
		if(student == null){
			addActionMessage("请使用学生账号登陆！");
			return SUCCESS;
		}
	//	selectManager.findBySelect(page, titile, teacherName, Constants.THESIS_FLAG_KAITI, Constants.THSIS_STAUTS_KAITI_BASE_AUDIT_REPORT);
	//	listAll = selectManager.findExaminationBy(page, titile, teacherName, Constants.THESIS_FLAG_KAITI, Constants.THSIS_STAUTS_KAITI_BASE_AUDIT_REPORT,null,null);
		thesis = student.getThesis();
		if(thesis == null){
			addActionMessage("你没有已选的题目");
			return SUCCESS;
		}
		thesisFile = thesis.getThesisFile();
		if(thesisFile.getThesisStatus() < Constants.THSIS_STAUTS_KAITI_BASE_AUDIT){
			addActionMessage("还未到写周记环节");
			return SUCCESS;
		}
		if(thesisFile.getThesisStatus() >= Constants.THSIS_STAUTS_MIDLLE_WEEK_REPORT){
			addActionMessage("你的周记已写完");
			return SUCCESS;
		}
		Struts2Utils.getRequest().setAttribute("student", student);
		return SUCCESS;
	}
	
	/**
	 * 初始化周记
	 * @return
	 */
	public String diary(){
		if(id == null || id == 0l){
			addActionMessage("非法操作");
			return RELOAD;
		}
		thesis = thesisManager.get(id);
		if(wid == null || wid == 0l){
			weeklydiary = new Weeklydiary();
		}else{
			weeklydiary = weeklydiaryManager.get(wid);
		}
		return DIARY;
	}
	
	/**
	 * 保存周记
	 * @return
	 */
	@LogAnnotation(log = "提交周记 - 保存周记")
	@Token
	public String save(){
		if(id == null || id == 0l){
			addActionMessage("非法操作");
			return RELOAD;
		}
		try {
			String content = weeklydiary.getDiaryContent();
			Student student = studentManager.findByUser(UserHolder.getLoginUser().getId());
			if(student == null){
				addActionMessage("请用学生账号登陆！");
				return RELOAD;
			}
			
			//如果，周记类型不是有效值  那么本次访问为视为非法
			if(Arrays.binarySearch(effievtiveValue,diaryType) == -1){
				addActionMessage("非法操作！");
				return RELOAD;
			}
			if(wid == null || wid == 0l){
				weeklydiary = new Weeklydiary();
				weeklydiary.setLastEditDate(new Date());
				weeklydiary.setLastEditStudent(student.getStudentName());
				weeklydiary.setDiaryName(diaryType +"~" +(diaryType + 2) +"周");
				weeklydiary.setDiaryType(diaryType);
				weeklydiary.setThesis(thesis);
				weeklydiary.setDiaryContent(content);
				//变动 客户说周记就有1-2和3-4了 ================
//				if(diaryType == 5L){
//					diaryType = 20L;
//				}
				//==============变动结束==========
				if(diaryType == 20L){
					weeklydiary.setDiaryName("总结");					
				}
				
			}else{
				weeklydiary = weeklydiaryManager.get(wid);
				weeklydiary.setDiaryContent(content);
				weeklydiary.setLastEditDate(new Date());
				weeklydiary.setLastEditStudent(student.getStudentName());
			}
			weeklydiaryManager.save(weeklydiary);
			
			if(diaryType == 20L){
				thesis = thesisManager.get(id);
				thesisFile = thesis.getThesisFile();
	//			thesisFile.setThesisStatus(Constants.THSIS_STAUTS_MIDLLE_WEEK_REPORT);
	//			thesisFile.setThesisFlag(Constants.THESIS_FLAG_MIDLLE);
				fileManager.save(thesisFile);
			}
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("保存失败！");
			return RELOAD;
		}
		return RELOAD;
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public Page<Thesis> getPage() {
		return page;
	}
	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Long getStauts() {
		return stauts;
	}
	public void setStauts(Long stauts) {
		this.stauts = stauts;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
	public String getTitile() {
		return titile;
	}
	public void setTitile(String titile) {
		this.titile = titile;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}
	@Autowired
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}

	public Long getWid() {
		return wid;
	}

	public void setWid(Long wid) {
		this.wid = wid;
	}

	public Weeklydiary getWeeklydiary() {
		return weeklydiary;
	}

	public void setWeeklydiary(Weeklydiary weeklydiary) {
		this.weeklydiary = weeklydiary;
	}
	@Autowired
	public void setWeeklydiaryManager(WeeklydiaryManager weeklydiaryManager) {
		this.weeklydiaryManager = weeklydiaryManager;
	}
	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	public Long getDiaryType() {
		return diaryType;
	}
	public void setDiaryType(Long diaryType) {
		this.diaryType = diaryType;
	}

	public List<Thesis> getListAll() {
		return listAll;
	}

	public void setListAll(List<Thesis> listAll) {
		this.listAll = listAll;
	}
}

